package com.it.gw.modules.station.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.StringUtils
import com.it.data.network.rsp.MyWorkOrderVo
import com.it.gw.R
import com.it.gw.databinding.ItemMyWorkBinding
import com.it.gw.manager.DictionariesManager


class MyWorkAdapter(
    var context: Context,
    var listener: (MyWorkOrderVo) -> Unit,
    var datas: List<MyWorkOrderVo>
) : RecyclerView.Adapter<MyWorkAdapter.ViewHolder>() {
    companion object {
        const val TAG = "MyWorkAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemMyWorkBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.STNM.text = StringUtils.getNoNullData(data.stnm)
        holder.binding.ADDRESS.text =
            "${StringUtils.getNoNullData(data.shi)}/${StringUtils.getNoNullData(data.xian)}"

        holder.binding.orderType.text =
            DictionariesManager.orderTypeList.find { it.first == StringUtils.getNoNullData(data.orderType) }?.second

        holder.binding.startTimeEndTime.text =
            "任务时间：${StringUtils.getNoNullData(data.startTime)}-${StringUtils.getNoNullData(data.endTime)}"
        if (data.status == "1") {
            holder.binding.status.text = "已\n完\n成"
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_green)
        } else {
            holder.binding.status.text = "未\n完\n成"
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_yellow)
        }

        holder.binding.root.setOnClickListener {
            listener.invoke(data)
        }

    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemMyWorkBinding) : RecyclerView.ViewHolder(binding.root)
}